﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.MediaCenter.UI;
using System.Diagnostics;
using MyMoviesBrowserLibrary.MyMovies;

namespace MyMoviesBrowser
{
    public class MovieGallery : GalleryPage
    {
        private MovieDataInterface movieData;

        private MyMovie currentMovie;
        public MyMovie CurrentMovie
        {
            get { return currentMovie; }
            set
            {
                if (currentMovie != value)
                {
                    currentMovie = value;
                    FirePropertyChanged("CurrentMovie");
                }
            }
        }

        List<MyMovie> movies = new List<MyMovie>();
        public List<MyMovie> Movies
        {
            get { return movies; }
        }

        public MovieGallery(List<MyMovie> moviesList)
        {
            ItemSize = GalleryItemSize.Large;
            Description = "Movies";

            movies = moviesList;

            movieData = new MyMoviesData();

            VirtualList galleryList = new VirtualList(this, null);
            galleryList.EnableSlowDataRequests = true;
            galleryList.RequestSlowDataHandler = new RequestSlowDataHandler(CompleteGalleryItem);
            Content = galleryList;

            CreateGalleryFilters();
        }

        private void CreateGalleryFilters()
        {
            //
            // Put together a list of filters on the content.
            //

            ArrayListDataSet list = movieData.CreateFilters(this);

            // Create the unfiltered "All" filter
            //ModelItem filter = new Filter(this, Resources.GroupBy_Title, 0);
            //list.Add(filter);

            //filter = new Filter(this, Resources.GroupBy_LastAdded, 5);
            //list.Add(filter);

            //filter = new Filter(this, Resources.FilterBy_NotWatched, 7);
            //list.Add(filter);

            //filter = new Filter(this, Resources.FilterBy_Rating, 8);
            //list.Add(filter);


            //filter = new Filter(this, Resources.GroupBy_MPAARating, 2);
            //list.Add(filter);

            //filter = new Filter(this, Resources.GroupBy_Genre, 3);
            //list.Add(filter);

            //filter = new Filter(this, Resources.GroupBy_Release_Date, 1);
            //list.Add(filter);
                        
            Choice filters = new Choice(this, null, list);
            if(list.Count > 0)
                filters.Chosen = (ModelItem)list[0];

            //
            // Hook up the "filter changed" event so that we can apply the
            // active filter to our gallery.
            //
            // As soon as this Filters list is set, we will get an OnActiveFilterChanged
            // event which will cause us to populate the gallery.
            //

            ActiveFilterChanged += delegate(object sender, EventArgs e)
            {
                MovieGallery galleryPage = (MovieGallery)sender;
                Filter activeFilter = (Filter)galleryPage.Filters.Chosen;
                FilterContent(galleryPage, activeFilter.FilterId);
            };
            Filters = filters;
        }

        private void FilterContent(MovieGallery page, int filterId)
        {
            Content.Clear();

            movieData.FilterContent(page, filterId);

            //
            // Populate the list with items according to the filter.
            //
            //sList<MyMovie> temp = null;
            
        }

        internal ThumbnailCommand CreateGalleryItem(MyMovie movieData, string description, bool isHeading)
        {
            ThumbnailCommand item = new ThumbnailCommand(this);
            item.Description = movieData.Title;
            item.Data = movieData;
            item.ShowHeading = isHeading;
            item.Metadata = description;
            //
            // Hook up an event for when the gallery item is invoked.
            //

            item.Invoked += delegate(object sender, EventArgs args)
            {
                ThumbnailCommand galleryItem = (ThumbnailCommand)sender;

                MyMovie movie = (MyMovie)galleryItem.Data;
                //MyMoviesBrowserLibrary.MyMoviesBrowserBusiness.MarkMovieAsWatched(movie.MovieTitle, true);
                Application.Current.GoToDetailsPage((MyMovie)galleryItem.Data);
            };

            item.OnFocus += delegate(object sender, EventArgs args)
            {
                ThumbnailCommand galleryItem = (ThumbnailCommand)sender;

                MovieGallery page = (MovieGallery)galleryItem.Owner;
                page.CurrentMovie = (MyMovie)galleryItem.Data;
            };

            return item;
        }

        public override int JumpTo(string searchText)
        {
            searchText = searchText.ToLower();
            List<MyMovie> temp = Content.Cast<ThumbnailCommand>().Select(t => t.Data).Cast<MyMovie>().ToList();
            List<MyMovie> temp2 = new List<MyMovie>();
            do
            {
                temp2 = (from m in temp
                         where m.SortTitle.ToLower().StartsWith(searchText)
                         select m).ToList();
                if (searchText.Length > 0)
                    searchText = searchText.Substring(0, searchText.Length - 1);
            } while (temp2.Count == 0 && searchText.Length > 0);

            if (temp2.Count > 0)
            {
                int i = temp.IndexOf(temp2.First());
                return i;
            }
            return -1;
        }

        #region slowdatahandlers
        /// <summary>
        /// Finishes any slow data for a gallery item.
        /// </summary>
        protected void CompleteGalleryItem(VirtualList list, int index)
        {
            ThumbnailCommand item = (ThumbnailCommand)list[index];
            CompleteGalleryItem(item, (MyMovie)item.Data);
        }

        /// <summary>
        /// Finishes any slow data for a gallery item.
        /// </summary>
        protected void CompleteGalleryItem(ThumbnailCommand item, MyMovie myMovieData)
        {
            string imageName = "file://" + myMovieData.FolderImage;
            item.Image = LoadImage(imageName);
        }
        #endregion
    }
}
